package handler

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/interface/dao"
	"golang-websocket-chatroom/interface/models"
)

func GroupCreate(context *gin.Context) {
	var req request.GroupsReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var user models.Users
	dao.FindLogin(req.Id, &user)
	if req.Id == 0 {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "用户不存在",
			"data": nil,
		})
		return
	}
	var group models.Groups
	dao.FindLogin(group.Id, &group)
	if group.Id != 0 {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "用户在群聊中",
			"data": nil,
		})
		return
	}

	groups := &models.Groups{
		Other: context.GetUint("userId"),
	}

	dao.Groups(groups, &models.Groups{
		Other: user.Id,
	})

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "用户进入",
		"data": group,
	})
}
